package com.eud.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class UserFilter
 */
@WebFilter(urlPatterns = "/*")
public class UserFilter implements Filter {

    /**
     * Default constructor. 
     */
    public UserFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpServletRequest hrequest = (HttpServletRequest)request;
		HttpServletResponse hresponse = (HttpServletResponse)response;
		String url = hrequest.getRequestURL().toString();
		if(url.contains("/login.jsp")|| url.contains("/deletAdminServlet")|| url.contains("/AddMovieServlet")|| url.contains("/admin_movieServlet")|| url.contains("/queryAllOrderServlet")|| url.contains("/UpdateAdminServlet")|| url.contains("/DeleteAllAdminServlet")|| url.contains("/AddAdminServlet")|| url.contains("/queryAllAdminServlet")|| url.contains("/queryAllServlet")|| url.contains("js/jquery-ui.css") ||url.contains("adminIndex.css")|| url.contains("css/adminIndex.css")|| url.contains("/crm_user.jsp")|| url.contains("/crm_index.jsp")|| url.contains("/admin_login.jsp")|| url.contains("/AdminLogServlet")|| url.contains("/DeleteAllServlet")|| url.contains("/AddServlet")|| url.contains("/UpdateServlet")|| url.contains("/SeatServlet")|| url.contains("/SeatFirstServlet")|| url.contains("/MovieFirstServlet") || url.contains(".png")|| url.contains(".jpg")|| url.contains("/LogServlet")|| url.contains("/RegisterServlet")|| url.contains("/register.jsp")|| url.contains("/index.jsp") || url.contains(".css") || url.contains(".js") ) {
			chain.doFilter(request, response);
			return;
		}
		HttpSession session = hrequest.getSession();
		if(session.getAttribute("user")== null) {
			hresponse.sendRedirect("login.jsp");
			return;
		}
		chain.doFilter(request, response);
		
	}
	

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
